//
//  HJGoodsAdressController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJGoodsAdressController.h"
#import "HJGoodsAdressCell.h"
#import "HJNewAddViewController.h"
#import "HJMineModel.h"

@interface HJGoodsAdressController ()
@property(nonatomic,strong) NSIndexPath * indexPath;
@property(nonatomic,strong) NSMutableArray <HJMyAddressModel*>* dataArray;
@end

@implementation HJGoodsAdressController

-(NSMutableArray <HJMyAddressModel*> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.rightNavTitle = @"新增";
    [self loadNetworkData];
}

-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    [self.tableView registerCellByNibName:@"HJGoodsAdressCell"];
}

-(void)loadNetworkData {
    [TZHttpTool post:MyAddressAPI params:nil success:^(NSDictionary *result) {
        NSArray *data = [HJMyAddressModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:data];
        [self.tableView reloadData];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataArray.count>0) {
        [self.tableView hideNoData];
         return self.dataArray.count;
    }else {
        [self.tableView showNoData]; return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJGoodsAdressCell * goodsAdressCell = [tableView dequeueReusableCellWithIdentifier:@"HJGoodsAdressCell"];
    goodsAdressCell.selectionStyle= UITableViewCellSelectionStyleNone;
    goodsAdressCell.myModel = self.dataArray[indexPath.row];
    return goodsAdressCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_enditBool) {
        return YES;
    }
    return NO;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJMyAddressModel * model = self.dataArray[indexPath.row];
    [TZHttpTool post:DeleteAddressAPI params:@{@"address_id":model.address_id} success:^(NSDictionary *result) {
        [self showHint:@"删除成功"];
        // 从数据源中删除
        [self.dataArray removeObjectAtIndex:indexPath.row];
        // 从列表中删除
//         [self.tableView deleteRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView deleteRow:indexPath.row inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    } failure:^(NSString *msg) {
        [self showSuccessHUDWithStr:msg];
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_enditBool) {
        return;
    }else {
        HJMyAddressModel * model = self.dataArray[indexPath.row];
        if (self.Blok) {
            self.Blok(model.name, [NSString stringWithFormat:@"%@%@",model.address,model.detail_address], model.mobile,model.address_id);
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }
}

-(void)didClickRightNavAction {
    HJNewAddViewController *newAddVC = [[HJNewAddViewController alloc]init];
    WeakSelf
    newAddVC.loadBlock = ^{
        [weakSelf loadNetworkData];
    };
    newAddVC.navTitle = @"新增地址";
    [self.navigationController pushViewController:newAddVC animated:YES];
}


@end
